﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Barbar.UrlGuard.Dal.Persist;
using Barbar.UrlGuard.Dal;
using System.Diagnostics;

namespace Barbar.UrlGuard.Test {
  [TestClass]
  public class BinaryFactoryTest {
    [TestMethod]
    public void TestMonitoringCheck() {
      MonitoringCheck check = new MonitoringCheck();
      check.CheckDate = new DateTime(1999, 2, 2);
      check.RequestLength = 2.67;
      check.StatusCode = 404;

      BinaryFactory binaryFactory = new BinaryFactory();
      IBinaryFactory<MonitoringCheck> iface = (IBinaryFactory<MonitoringCheck>)binaryFactory;
      byte[] buffer = iface.GetBytes(check);
      MonitoringCheck deserializedCheck = iface.GetObject(buffer);

      Assert.AreEqual(check.CheckDate, deserializedCheck.CheckDate);
      Assert.AreEqual(check.RequestLength, deserializedCheck.RequestLength);
      Assert.AreEqual(check.StatusCode, deserializedCheck.StatusCode);
    }

    [TestMethod]
    public void TestIndexEntry() {
      Guid guid = new Guid("9A3938E2-C803-4e90-890A-84D95FF85D6A");
      IndexEntry entry = new IndexEntry(guid, 123456789);

      BinaryFactory binaryFactory = new BinaryFactory();
      IBinaryFactory<IndexEntry> iface = (IBinaryFactory<IndexEntry>)binaryFactory;
      byte[] buffer = iface.GetBytes(entry);
      IndexEntry deserializedEntry = iface.GetObject(buffer);

      Assert.AreEqual(entry.FilePosition, deserializedEntry.FilePosition);
      Assert.AreEqual(entry.RecordType, deserializedEntry.RecordType);
    }

    [TestMethod]
    public void TestRecordHeader() {
      Guid guid = new Guid("9A3938E2-C803-4e90-890A-84D95FF85D6A");
      RecordHeader header = new RecordHeader();
      header.ChunkSize = 4124;
      header.NextRecordPosition = 123456789;
      header.RecordsCount = 7777;
      header.RecordSize = 8080;
      header.RecordType = guid;

      BinaryFactory binaryFactory = new BinaryFactory();
      IBinaryFactory<RecordHeader> iface = (IBinaryFactory<RecordHeader>)binaryFactory;
      byte[] buffer = iface.GetBytes(header);
      RecordHeader deserializedHeader = iface.GetObject(buffer);

      Assert.AreEqual(header.ChunkSize, deserializedHeader.ChunkSize);
      Assert.AreEqual(header.NextRecordPosition, deserializedHeader.NextRecordPosition);
      Assert.AreEqual(header.RecordsCount, deserializedHeader.RecordsCount);
      Assert.AreEqual(header.RecordSize, deserializedHeader.RecordSize);
      Assert.AreEqual(header.RecordType, deserializedHeader.RecordType);
    }
  }
}
